﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Memcached.ClientLibrary;
using System.Collections;

namespace NHibernate.Comm.memcached
{
    public class MemcachedTools:IMemcachedCache
    {
        static string cacheName = Guid.NewGuid().ToString();
        static MemcachedTools()
        {
            string servers = System.Configuration.ConfigurationManager.AppSettings["Memcached"];
            if (!string.IsNullOrEmpty(servers))
            {
                String[] serverlist = servers.Split('|');
                SockIOPool pool = SockIOPool.GetInstance(cacheName);
                pool.SocketConnectTimeout = 5000000;
                pool.SocketTimeout = 500000;
                pool.SetServers(serverlist);
                pool.Initialize();
            }
        }

        public object GetClient()
        {
            MemcachedClient mc = new MemcachedClient();
            mc.PoolName = cacheName;
            mc.EnableCompression = false;
            return mc; 
        }

        string GetKey(string key)
        {
            return "UserTraining"+ key;
        }

        public void Add(string key, object obj, int hours, int minutes, int seconds)
        {
            key = GetKey(key);
            MemcachedClient cache = (MemcachedClient)GetClient();
            if (!cache.Add(key, obj, DateTime.Now.Add(new TimeSpan(hours, minutes, seconds))))
            {
                var result = cache.Set(key, obj, DateTime.Now.Add(new TimeSpan(hours, minutes, seconds)));
            }
        }

        public void Remove(string key)
        {
            key = GetKey(key);
            MemcachedClient cache = (MemcachedClient)GetClient();
            cache.Delete(key);
        }

        public void FlushAll()
        {
            MemcachedClient cache = (MemcachedClient)GetClient();
            cache.FlushAll();
        }

        public T Get<T>(string key)
        {
            key = GetKey(key);
            MemcachedClient cache = (MemcachedClient)GetClient();
            try
            {
                return (T)cache.Get(key);
            }
            catch
            {
                return default(T);
            }
        }

        public Hashtable GetValues(string[] keys)
        {
            if (keys == null || keys.Length == 0)
            {
                return new Hashtable();
            }
            Dictionary<string, string> dicKeys = new Dictionary<string, string>();
            foreach (var key in keys)
            {
                dicKeys.Add(GetKey(key), key);
            }
            MemcachedClient cache = (MemcachedClient)GetClient();
            var tempKeys = dicKeys.Keys.ToArray();
            var keyValues = cache.GetMultiple(tempKeys);
            Hashtable ht = new Hashtable();
            foreach (string key in keyValues.Keys)
            {
                ht.Add(dicKeys[key], keyValues[key]);
            }
            return ht;
        }
 
    }
}
